package com.bruan.doudizhu;

import java.util.*;

public class Room {
    private List<Card> allCard = new ArrayList<>();

    public List<Card> getAllCard() {
        return allCard;
    }

    public void setAllCard(List<Card> allCard) {
        this.allCard = allCard;
    }

    public Room(){
        //创建初始的牌 点数确定 类型确定
        String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        //花色
        int size = 0;//表示牌的大小
        String[] colors = {"♠","♣","♥","♦"};
        for (String number:numbers){
            size++;
            for (String color :colors){
                //得到一张牌
                Card card = new Card(number,color,size);
                allCard.add(card);
            }
        }
        //单独传入大小王
        Card card = new Card("","小王",++size);
        Card card_ = new Card("","大王",++size);
        Collections.addAll(allCard,card,card_);
    }

    /**
     * 游戏启动
     */
    public void start() {
        System.out.println("洗牌前："+allCard);
        //1.洗牌
        Collections.shuffle(allCard);
        System.out.println("洗牌后："+allCard);
        //2.发牌
        List<Card> lkc = new ArrayList<>();
        List<Card> lkh = new ArrayList<>();
        List<Card> zyb = new ArrayList<>();
        //正式发牌给玩家（51）
        for (int i = 0; i < allCard.size()-3; i++) {
            Card c = allCard.get(i);
            if(i%3==0){
                lkc.add(c);
            }else if (i%3==1){
                lkh.add(c);
            }else{
                zyb.add(c);
            }
        }
        //对牌进行排序
        sortCard(lkh);
        sortCard(lkc);
        sortCard(zyb);
        //查看牌
        System.out.println("----查看牌---");
        System.out.println("lkc:"+lkc);
        System.out.println("lkh:"+lkh);
        System.out.println("zyb:"+zyb);
        //获取最后三张卡
        List<Card> lastTreeCard=allCard.subList(allCard.size()-3,allCard.size());
        System.out.println("最后三张底牌"+lastTreeCard);
        //抢地主
        getDizhu(lastTreeCard,lkc,lkh,zyb);
        //查看牌
        System.out.println("----开始抢地主---");
        System.out.println("lkc:"+lkc);
        System.out.println("lkh:"+lkh);
        System.out.println("zyb:"+zyb);

    }

    private void sortCard(List<Card> cards) {
        Collections.sort(cards, new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                return o1.getSize()-o2.getSize();
            }
        });
    }
    private void getDizhu(List<Card> lastThreeCards,List<Card>... threePersonCards){
        Random r = new Random();
        int i = r.nextInt(3);
        if (i==0){
            System.out.println("地主是lkc");
            threePersonCards[0].addAll(lastThreeCards);
            sortCard(threePersonCards[0]);
        }else if(i==1){
            System.out.println("地主是lkh");
            threePersonCards[1].addAll(lastThreeCards);
            sortCard(threePersonCards[1]);
        }else if (i==2){
            System.out.println("地主是zyb");
            threePersonCards[2].addAll(lastThreeCards);
            sortCard(threePersonCards[2]);
        }
    }

}
